package viv.karaoke.book;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class SettingFragment extends Fragment {

	private SeekBar seekBarBrightness;
	private EditText txtBrightness;
	private SeekBar seekBarIdle;
	private EditText txtIdleTime;
	
	private MainActivity activity;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.setting, container, false);

		txtBrightness = (EditText)view.findViewById(R.id.txtBrightness);
		seekBarBrightness = (SeekBar)view.findViewById(R.id.seekBarBrightness);
		
		txtIdleTime = (EditText)view.findViewById(R.id.txtIdleTime);
		seekBarIdle = (SeekBar)view.findViewById(R.id.seekBarIdle);

		return view;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		activity = (MainActivity)getActivity();
	    
	    if (activity != null) {
	    	
	    	seekBarBrightness.setProgress(activity.getBrightnessValue());
	    	txtBrightness.setText(Integer.toString(activity.getBrightnessValue()));
	    	
	    	seekBarIdle.setProgress(activity.getIdleTime());
	    	txtIdleTime.setText(Integer.toString(activity.getIdleTime()));
	    	
	    	seekBarBrightness.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
				public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
					// TODO Auto-generated method stub
					
					txtBrightness.setText(Integer.toString(progress + 1));
					
//					Settings.System.putInt(y.getContentResolver(),Settings.System.SCREEN_BRIGHTNESS_MODE, 0); 
//					Settings.System.putInt(y.getContentResolver(),Settings.System.SCREEN_BRIGHTNESS, y.brightness1);
					
					WindowManager.LayoutParams lp = activity.getWindow().getAttributes(); 
					lp.screenBrightness = (float) ((progress + 1) * 0.1); 
					activity.getWindow().setAttributes(lp);
				}

				public void onStartTrackingTouch(SeekBar seekBar) {
					// TODO Auto-generated method stub
				}

				public void onStopTrackingTouch(SeekBar seekBar) {
					// TODO Auto-generated method stub
				}
			});
	    	
	    	seekBarIdle.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
				public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
					// TODO Auto-generated method stub
					
					txtIdleTime.setText(Integer.toString(progress + 1));
					
//					Settings.System.putInt(y.getContentResolver(),Settings.System.SCREEN_BRIGHTNESS_MODE, 0); 
//					Settings.System.putInt(y.getContentResolver(),Settings.System.SCREEN_BRIGHTNESS, y.brightness1);
					
					
				}

				public void onStartTrackingTouch(SeekBar seekBar) {
					// TODO Auto-generated method stub
				}

				public void onStopTrackingTouch(SeekBar seekBar) {
					// TODO Auto-generated method stub
				}
			});
	    }
		
	}
}
